//
//  RadioTableViewCell.m
//  A_program
//
//  Created by BayMax on 15/5/23.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "RadioTableViewCell.h"
#import "RadioThirdButton.h"
#import "NovelModel.h"
#import "NovelDatabase.h"
@implementation RadioTableViewCell

- (void)dealloc
{
    [_nameLabel release];
    [_countLabel release];
    [_updateLabel release];
    [_listImageView release];
    [super dealloc];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
        
    }
    
    return self;
}

- (void)createView
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(125, 5, 260, 20)];
    //        [self.nameLabel setBackgroundColor:[UIColor greenColor]];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel release];
    
    UIImageView *titleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(95, 5, 25, 25)];
    [titleImageView setImage:[UIImage imageNamed:@"tee.png"]];
    [self.contentView addSubview:titleImageView];
    [titleImageView release];
    
    self.countLabel = [[UILabel alloc]initWithFrame:CGRectMake(125, 50, 80, 20)];
    //        [self.countLabel setBackgroundColor:[UIColor orangeColor]];
    self.countLabel.alpha = 0.4;
    [self.contentView addSubview:self.countLabel];
    [self.countLabel release];
    
    UIImageView *countImageView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 50, 15 , 15)];
    [countImageView setImage:[UIImage imageNamed:@"trigon.tiff"]];
    countImageView.alpha = 0.5;
    [self.contentView addSubview:countImageView];
    [countImageView release];
    
    self.updateLabel = [[UILabel alloc]initWithFrame:CGRectMake(125, 70, 150, 20)];
    //        [self.updateLabel setBackgroundColor:[UIColor redColor]];
    [self.contentView addSubview:self.updateLabel];
    [self.updateLabel release];
    
    self.listImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, self.frame.size.width / 4, 80)];
    //        [self.listImageView setBackgroundColor:[UIColor purpleColor]];
    [self.contentView addSubview:self.listImageView];
    [self.listImageView release];
    
    self.collectButton = [[RadioThirdButton alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/12*10,[UIScreen mainScreen].bounds.size.height*0.1, 50, 30)];
    self.collectButton.layer.borderColor = [UIColor orangeColor].CGColor;
    self.collectButton.layer.borderWidth = 1.0f;
    self.collectButton.layer.cornerRadius = 5.0f;
    self.collectButton.layer.masksToBounds = YES;
    self.collectButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.collectButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];

    [self.collectButton setTitle:@"收藏" forState:UIControlStateNormal];
    [self.collectButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.collectButton setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [self.collectButton addTarget:self action:@selector(collectButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.collectButton];
    [self.collectButton release];
    
}


- (void)cellButton:(NSString *)str{
    
    //按钮的id
    self.collectButton.radioThirdButtonId = str;
    //功能
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:[str integerValue] loginName:[myLoginName objectForKey:@"loginName"]];
    if (array.count == 0) {
        [self.collectButton setTitle:@"收藏" forState:UIControlStateNormal];
        
    }else{
        [self.collectButton setTitle:@"已收藏" forState:UIControlStateNormal];
    }

    
    
    
}
#pragma mark -
#pragma mark 收藏按钮
- (void)collectButtonAction:(id)sender{
#pragma mark-
#pragma mark 判断是否已收藏
    NSUserDefaults *myLoginName = [NSUserDefaults standardUserDefaults];
    NSInteger myAlbumId = [self.collectButton.radioThirdButtonId integerValue];
    NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:myAlbumId loginName:[myLoginName objectForKey:@"loginName"]];
    if ([array count] == 0 && [self.collectButton.titleLabel.text isEqualToString:@"收藏"]) {
        
        [self.radioThirdButtonDelegate createRadioThirdButtonAction:self.collectButton.radioThirdButtonId];
        if (![[myLoginName objectForKey:@"loginName"] length] == 0 &&[[myLoginName objectForKey:@"state"]isEqualToString:@"登录"]) {
            [self.collectButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }else{
            NSLog(@"未登录或则没有用户");
            
            
        }
    }else if([self.collectButton.titleLabel.text isEqualToString:@"已收藏"]){
                NovelModel *myid = [[NovelModel alloc]init];
        for (NovelModel *model in array) {
            myid = model;
        }
        [NovelDatabase deleteWithModel:myid];[self.collectButton setTitle:@"收藏" forState:UIControlStateNormal];

        
    }else{
        NSMutableArray *array = (NSMutableArray *)[NovelDatabase selectNameAlbumId:[self.collectButton.radioThirdButtonId integerValue] loginName:[myLoginName objectForKey:@"loginName"]];
        if ( ![array count] == 0) {
            [self.collectButton setTitle:@"已收藏" forState:UIControlStateNormal];
        }
    }

    
 
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
